package com.aiju.cash.ui.stockupdate.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.StockApi;
import com.aiju.cash.ui.stockupdate.bean.Stock;
import com.aiju.common.event.EventCenter;
import com.aiju.common.image.AiJuImageLoaderProxy;
import com.aiju.utils.KeyBoardUtils;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemTextView;

/**
 * @author: 佐为
 * @date: 2017-07-05 10:20
 * @version: V1.0
 * @Description: 库存信息
 */
public class StockInfoActivity extends BaseTitleActivity {

    private EditText editText;
    private ImageView stroeImageView;
    private SettingItemEditView stroeInventory;
    private SettingItemTextView storeName, storeWeight, stroeQr, storePrice;

    private Stock.GoodsListBean mGoodsListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_info);
        setCenterTitle("库存详情");

        mGoodsListBean = (Stock.GoodsListBean) getIntent().getSerializableExtra("goodsListBean");
        storeName = (SettingItemTextView) findViewById(R.id.set_stock_name);
        storeWeight = (SettingItemTextView) findViewById(R.id.set_stock_weight);
        stroeQr = (SettingItemTextView) findViewById(R.id.set_stock_qr);
        storePrice = (SettingItemTextView) findViewById(R.id.set_stock_price);
        stroeInventory = (SettingItemEditView) findViewById(R.id.set_stock_inventory);
        stroeInventory.setRightInputType(InputType.TYPE_CLASS_NUMBER);
        editText = (EditText) stroeInventory.getRightView();
        editText.setHint("请设置库存数量");
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String temp = s.toString();
                int posDot = temp.indexOf("0");
                if (s.length() == 2 && posDot == 0) {
                    editText.setText("0");
                    editText.setSelection(1);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        stroeImageView = (ImageView) findViewById(R.id.iv_store_pic);
        findViewById(R.id.iv_store_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setVisibility(View.GONE);
                stroeInventory.setEditable(true);
                findViewById(R.id.tv_store_save).setVisibility(View.VISIBLE);
                if (mGoodsListBean.getSurplus().equals("0")) {
                    editText.setText("");
                } else {
                    editText.setSelection(stroeInventory.getRightText().length());
                }
                KeyBoardUtils.openKeybord((EditText) stroeInventory.getRightView(), StockInfoActivity.this);
            }
        });
        findViewById(R.id.tv_store_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(stroeInventory.getRightText())) {
                    T.ToastShort("请设置库存数量");
                    return;
                }
                v.setVisibility(View.GONE);
                stroeInventory.setEditable(false);
                findViewById(R.id.iv_store_edit).setVisibility(View.VISIBLE);
                KeyBoardUtils.closeKeybord((EditText) stroeInventory.getRightView(), StockInfoActivity.this);
                inventory(Integer.valueOf(stroeInventory.getRightText()));
            }
        });
        storeName.setLeftTextBold();
        storeWeight.setLeftTextBold();
        stroeQr.setLeftTextBold();
        storePrice.setLeftTextBold();
        stroeInventory.setLeftTextBold();
        setStockInfo();
    }

    /**
     * 设置库存
     *
     * @param inventory
     */
    private void inventory(final int inventory) {
        new StockApi(mDialogHelper).updateInventory(mGoodsListBean.getId(), inventory, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("库存数量已更新");
                mGoodsListBean.setSurplus(String.valueOf(inventory));
                //mGoodsListBean.setInventory(inventory);
                EventCenter.getInstance().post(mGoodsListBean);
            }
        });
    }

    /**
     * 设置库存信息
     */
    private void setStockInfo() {
        if (mGoodsListBean == null) {
            return;
        }
        storeName.setRightText(mGoodsListBean.getName());
        stroeQr.setRightText(mGoodsListBean.getCode());
        storeWeight.setRightText(mGoodsListBean.getUnit().equals("件") ? "计件" : "称重");
        if (!mGoodsListBean.getUnit().equals("件")) {
            storePrice.setRightText("￥" + PriceUtil.formatDouble(String.valueOf(mGoodsListBean.getPrice()))
                    + "/" + mGoodsListBean.getUnit());
        } else {
            storePrice.setRightText("￥" + PriceUtil.formatDouble(String.valueOf(mGoodsListBean.getPrice())));
        }
        stroeInventory.setRightText(String.valueOf(mGoodsListBean.getSurplus()));
        AiJuImageLoaderProxy.getInstance().displayImage(mGoodsListBean.getImageUrl(), stroeImageView, R.mipmap.ic_stock_empty);
    }
}
